package com.eostoken.eos_wallet.ui.fragment;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;

import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatTextView;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.BusUtils;
import com.blankj.utilcode.util.FragmentUtils;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.StringUtils;
import com.eostoken.eos_wallet.MainActivity;
import com.eostoken.eos_wallet.R;
import com.eostoken.eos_wallet.base.BaseFragment;
import com.eostoken.eos_wallet.bean.CreateAccountMode;
import com.eostoken.eos_wallet.bus.ToWallet;
import com.eostoken.eos_wallet.db.MgpWallet;
import com.eostoken.eos_wallet.utils.Constants;
import com.eostoken.eos_wallet.utils.WalletUtils;
import com.fredy.mvp.BasePresenter;
import com.fredy.mvp.BaseView;
import com.fredy.mvp.Model;
import com.qmuiteam.qmui.widget.QMUITopBarLayout;
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.bingoogolapple.qrcode.zxing.QRCodeEncoder;
import party.loveit.bip44forandroidlibrary.utils.Bip44Utils;
import party.loveit.eosforandroidlibrary.Ecc;

import static com.eostoken.eos_wallet.utils.Constants.ADD_WALLET;
import static com.eostoken.eos_wallet.utils.Constants.BUS_TO_WALLET;
import static com.eostoken.eos_wallet.utils.Constants.EXTRA_ACCOUNT_NAME;
import static com.eostoken.eos_wallet.utils.Constants.EXTRA_HINT;
import static com.eostoken.eos_wallet.utils.Constants.EXTRA_WALLET;
import static com.eostoken.eos_wallet.utils.Constants.EXTRA_WALLET_PASSWORD;
import static com.eostoken.eos_wallet.utils.Constants.MGP_SYMBOL;
import static com.eostoken.eos_wallet.utils.Constants.TO_CREATE_WALLET;

public class ActiveFragment extends BaseFragment {

    @BindView(R.id.topbar)
    QMUITopBarLayout topbar;
    @BindView(R.id.logoIv)
    AppCompatImageView logoIv;
    @BindView(R.id.qrCodeIv)
    AppCompatImageView qrCodeIv;
    @BindView(R.id.accountNameTv)
    AppCompatTextView accountNameTv;
    @BindView(R.id.ownerPublickeyTv)
    AppCompatTextView ownerPublickeyTv;
    @BindView(R.id.activePublickeyTv)
    AppCompatTextView activePublickeyTv;
    @BindView(R.id.inviteFriendActiveBtn)
    QMUIRoundButton inviteFriendActiveBtn;

    private MgpWallet mgpWallet;
    private String publickey;
    private String accountName;
    private List<String> originList;
    private String password;
    private String hint;
    private Bitmap qrCodeBitmap;
    private String json;
    private boolean isCreate;
    private String privatekey;

    @Override
    protected int getContentLayout() {
        return R.layout.fragment_activate;
    }

    @Override
    protected void initData() {
        Bundle bundle = getArguments();
        isCreate = bundle.getBoolean("isCreate", false);
        mgpWallet = bundle.getParcelable(EXTRA_WALLET);
        publickey = mgpWallet.getPublicKey();
        accountName = mgpWallet.getCurrentAccountName();
        CreateAccountMode createAccountMode = new CreateAccountMode();
        createAccountMode.setAccount(accountName);
        createAccountMode.setActive(publickey);
        createAccountMode.setBlockchain(MGP_SYMBOL);
        createAccountMode.setOwner(publickey);
        createAccountMode.setType(TO_CREATE_WALLET);
        json = GsonUtils.toJson(createAccountMode);
        qrCodeBitmap = QRCodeEncoder.syncEncodeQRCode(json, SizeUtils.getMeasuredHeight(qrCodeIv));
    }

    @Override
    protected void initView(View contentView) {
        topbar.setTitle(StringUtils.getString(R.string.str_active_accountname));
        topbar.addLeftBackImageButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (getFragmentManager().getBackStackEntryCount() < 1) {
                    finish();
                } else {
                    FragmentUtils.pop(getFragmentManager());
                }
            }
        });
        qrCodeIv.setImageBitmap(qrCodeBitmap);
        accountNameTv.setText(accountName);
        ownerPublickeyTv.setText(publickey);
        activePublickeyTv.setText(publickey);
    }

    @OnClick(R.id.inviteFriendActiveBtn)
    public void onViewClicked() {
        doAfterPermissionsGranted();
    }

    @Override
    public Model createModel() {
        return null;
    }

    @Override
    public BaseView createView() {
        return null;
    }

    @Override
    public BasePresenter createPresenter() {
        return null;
    }

    @Override
    public void finishRefresh() {

    }

    private void doAfterPermissionsGranted() {
        Intent intent = new Intent(getActivity(), MainActivity.class);
        intent.putExtra("type", "create_wallet");
        ActivityUtils.startActivity(intent);
        if (isCreate) {
            BusUtils.post(BUS_TO_WALLET, new ToWallet(mgpWallet, ADD_WALLET));
        }
        FragmentUtils.removeAll(getFragmentManager());
        finish();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (qrCodeBitmap != null) {
            qrCodeBitmap.recycle();
            qrCodeBitmap = null;
        }
    }
}
